//
//  AgoraUtil.m
//  MyJSGame
//
//  Created by 张路欣 on 2017/5/31.
//
//

#import "AgoraUtil.h"

@implementation ADD_CLASS(AgoraUtil)

ADD_CLASS(AgoraUtil) *agoraUtil = nil;

+ (ADD_CLASS(AgoraUtil) *) ADD_FUN(getInstance) {
    if (agoraUtil == nil) {
        agoraUtil = [[ADD_CLASS(AgoraUtil) alloc] init];
    }
    return agoraUtil;
}

- (void) ADD_FUN(registerAgroaWithKey):(NSString *)key view:(UIView *)view {
    self.agoraKit = [AgoraRtcEngineKit sharedEngineWithAppId:key delegate:self];
    [self.agoraKit setChannelProfile:AgoraRtc_ChannelProfile_Communication];
    [self.agoraKit enableVideo];
    [self.agoraKit setVideoProfile:AgoraRtc_VideoProfile_120P swapWidthAndHeight:NO];
    self.view = view;
}

+ (void) ADD_FUN(initVideoView):(NSString *)viewData {
    NSError *error;
    NSData *data = [viewData dataUsingEncoding:NSUTF8StringEncoding];
    [ADD_CLASS(AgoraUtil) ADD_FUN(getInstance)].viewDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
    [[ADD_CLASS(AgoraUtil) ADD_FUN(getInstance)] ADD_FUN(initAllViews)];
}

- (void) ADD_FUN(releaseAllViews) {
    if (self.allViews != nil && self.allViews.count > 0) {
        for (NSString *key in self.allViews) {
            UIView *view = self.allViews[key];
            [view removeFromSuperview];
            [view release];
        }
    }
    if (self.allCanvas != nil && self.allCanvas.count > 0) {
        for (NSString *key in self.allCanvas) {
            UIView *canvas = self.allCanvas[key];
            [canvas release];
        }
    }
    self.allViews = nil;
    self.allCanvas = nil;
}

- (void) ADD_FUN(initAllViews) {
    [[ADD_CLASS(AgoraUtil) ADD_FUN(getInstance)] ADD_FUN(releaseAllViews)];
    self.allViews = [[NSMutableDictionary alloc] init];
    self.allCanvas = [[NSMutableDictionary alloc] init];
    for (NSString *key in self.viewDict) {
        NSDictionary *_dict = [self transPos:self.viewDict[key]];
        double x = [_dict[@"x"] doubleValue];
        double y = [_dict[@"y"] doubleValue];
        double width = [_dict[@"width"] doubleValue];
        double height = [_dict[@"height"] doubleValue];
        UIView *videoView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        videoView.translatesAutoresizingMaskIntoConstraints = NO;
        videoView.backgroundColor = [UIColor clearColor];
        videoView.userInteractionEnabled = NO;
        AgoraRtcVideoCanvas *canvas = [[AgoraRtcVideoCanvas alloc] init];
        canvas.uid = [key intValue];
        canvas.view = videoView;
        canvas.renderMode = AgoraRtc_Render_Hidden;
        [self.view addSubview:videoView];
        [self.allViews setObject:videoView forKey:key];
        [self.allCanvas setObject:canvas forKey:key];
    }
}

- (NSDictionary *)transPos:(NSDictionary *)_dict {
    double x = [_dict[@"x"] doubleValue];
    double y = [_dict[@"y"] doubleValue];
    double width = [_dict[@"width"] doubleValue];
    double height = [_dict[@"height"] doubleValue];
    double frame_width = self.view.frame.size.width;
    double frame_height = self.view.frame.size.height;
    double scale = frame_width / 1280;
    NSDictionary *_newdic = @{@"x": [NSString stringWithFormat:@"%f", x * scale],
            @"y": [NSString stringWithFormat:@"%f", (720 - y) * scale],
            @"width": [NSString stringWithFormat:@"%f", width * scale],
            @"height": [NSString stringWithFormat:@"%f", height * scale]};
    return _newdic;
}

- (bool)loadAgoraKit:(NSString *)roomid uid:(NSString *)uid {
    int code = -1;
    AgoraRtcVideoCanvas *canvas = [self.allCanvas objectForKey:uid];
    if (canvas != nil) {
        [self.agoraKit setupLocalVideo:canvas];
        [self.agoraKit startPreview];
        code = [self.agoraKit joinChannelByKey:nil channelName:roomid info:nil uid:[uid intValue] joinSuccess:nil];
    }
    return code == 0;
}

- (void)rtcEngine:(AgoraRtcEngineKit *)engine firstRemoteVideoDecodedOfUid:(NSUInteger)uid size:(CGSize)size elapsed:(NSInteger)elapsed {
    AgoraRtcVideoCanvas *canvas = [self.allCanvas objectForKey:[NSString stringWithFormat:@"%lu", (unsigned long) uid]];
    if (canvas != nil) {
        [self.agoraKit setupRemoteVideo:canvas];
    }
}

+ (bool) ADD_FUN(openVideo):(NSString *)roomid uid:(NSString *)uid {
    return [[ADD_CLASS(AgoraUtil) ADD_FUN(getInstance)] loadAgoraKit:roomid uid:uid];
}

+ (void) ADD_FUN(hideAllVideo) {
    if ([ADD_CLASS(AgoraUtil) ADD_FUN(getInstance)].allViews != nil && [ADD_CLASS(AgoraUtil) ADD_FUN(getInstance)].allViews.count > 0) {
        for (NSString *key in [ADD_CLASS(AgoraUtil) ADD_FUN(getInstance)].allViews) {
            UIView *view = [ADD_CLASS(AgoraUtil) ADD_FUN(getInstance)].allViews[key];
            [view setHidden:YES];
        }
    }
}

+ (void) ADD_FUN(showAllVideo) {
    if ([ADD_CLASS(AgoraUtil) ADD_FUN(getInstance)].allViews != nil && [ADD_CLASS(AgoraUtil) ADD_FUN(getInstance)].allViews.count > 0) {
        for (NSString *key in [ADD_CLASS(AgoraUtil) ADD_FUN(getInstance)].allViews) {
            UIView *view = [ADD_CLASS(AgoraUtil) ADD_FUN(getInstance)].allViews[key];
            [view setHidden:NO];
        }
    }
}

+ (void) ADD_FUN(closeVideo) {
    [[ADD_CLASS(AgoraUtil) ADD_FUN(getInstance)].agoraKit setupLocalVideo:nil];
    [[ADD_CLASS(AgoraUtil) ADD_FUN(getInstance)].agoraKit leaveChannel:nil];
    [[ADD_CLASS(AgoraUtil) ADD_FUN(getInstance)].agoraKit stopPreview];
    [[ADD_CLASS(AgoraUtil) ADD_FUN(getInstance)] ADD_FUN(releaseAllViews)];
}

@end
